package com.wodwl.dao;

import java.io.IOException;

import java.io.Reader;

import java.util.List;

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.wodwl.entity.Category;

public class CategoryDao {

	public List<Category> getCategorysByUidType(Category category) {
		SqlMapClient smc = null;
		List<Category> Categorys=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Categorys =(List<Category>) smc.queryForList("category.getCategorysByUidType", category);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return Categorys;

	}

	public void addCountByCid(int cid) {

		SqlMapClient smc = null;

		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			smc.update("category.addCountByCid", cid);
			smc.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			endTransaction(smc);
		}
	}
	public void modifyCNameByCid(Category category) {

		SqlMapClient smc = null;

		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			smc.update("category.modifyCNameByCid", category);
			smc.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			endTransaction(smc);
		}
	}
	public void insert(Category category) {

		SqlMapClient smc = null;

		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			smc.insert("category.insert", category);
			smc.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			endTransaction(smc);
		}
	}
	public void deleteByCid(int cid) {

		SqlMapClient smc = null;

		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			smc.delete("category.deleteByCid", cid);
			smc.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			endTransaction(smc);
		}
	}
	
	public void deleteByUid(Category category) {

		SqlMapClient smc = null;

		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			smc.delete("category.deleteByUid", category);
			smc.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			endTransaction(smc);
		}
	}
	// //
	// ////////////////////////////////////////////////////////////////////////

	private SqlMapClient getSqlMapClient() throws IOException {

		// 初始化ibatis, 获得一个SqlMapClient对象

		String resource = "ibatis/SqlMapConfig.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		return SqlMapClientBuilder.buildSqlMapClient(reader);

	}

	private void endTransaction(SqlMapClient smc) {

		if (smc == null) {
			return;
		}
		try {
			smc.endTransaction();
		} catch (Exception e2) {
			e2.printStackTrace();
		}

	}

	public static void main(String[] args) {
		CategoryDao dao = new CategoryDao();
		Category category=new Category();
		dao.addCountByCid(32);
		
		/*
		 //增
		category.setUid(1);
		category.setType(Category.Type.RESOURCE);
		category.setOid(4);
		category.setCname("two");
		dao.insert(category);
		
		*/
		/*
		
		//查
		category.setUid(1);
		category.setType(Category.Type.BLOG);
		List<Category> list=dao.getCategorysByUidType(category);
		System.out.println(list.size());
		*/
		
		/*
		//改
		category.setCid(12);
		category.setCname("hello");
		dao.modifyCNameByCid(category);
		*/
		
		/*
		//改
		category.setCid(5);
		category.setOid(111);
		dao.modifyOidByCid(category);
		*/
		
		//删
		//dao.deleteByCid(5);
		
		//List list=dao.getAllCategorys();
		
	}

}